package com.hnzbxy.api.biz.controller;

import com.hnzbxy.api.biz.entity.ClassAdvisor;
import com.hnzbxy.api.biz.service.ClassAdvisorService;
import com.hnzbxy.api.biz.vo.ClassAdvisorVO;
import com.hnzbxy.api.common.annotation.ControllerEndpoint;
import com.hnzbxy.api.common.bean.ResponseBean;
import com.hnzbxy.api.common.pojo.system.User;
import com.hnzbxy.api.system.vo.PageVO;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 班主任管理接口（对齐UserController风格：分页+条件查询）
 * @Author xiaoge
 * @Date 2020/3/10 11:51
 * @Version 1.0
 **/
@Api(tags = "班主任管理接口")
@RequestMapping("/classAdvisor")
@RestController
@CrossOrigin
@Validated // 开启参数校验（与UserController一致）
public class ClassAdvisorController {

    @Autowired
    private ClassAdvisorService classAdvisorService;

    /**
     * 班主任列表（分页+条件查询）
     * 匹配前端Vue查询字段：姓名、性别、手机号、邮箱
     */
    @ApiOperation(value = "班主任列表", notes = "分页查询班主任列表，支持姓名/性别/手机号/邮箱条件筛选")
    @RequiresPermissions({"classAdvisor:list"})
    @GetMapping("/list")
    // 1. 分页参数：默认pageNum=1，pageSize=7（与UserController一致）
    // 2. 条件参数：ClassAdvisorVO接收前端查询条件（姓名/性别/手机号/邮箱）
    public ResponseBean list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                             ClassAdvisorVO classAdvisorVO) {
        // 调用Service的分页+条件查询方法（需在Service中新增此方法）
        PageVO<ClassAdvisorVO> pageVO = classAdvisorService.findClassAdvisorList(pageNum, pageSize, classAdvisorVO);
        return ResponseBean.success(pageVO); // 返回PageVO，包含total和rows
    }

    /**
     * 新增班主任（保持原有逻辑，仅优化参数校验注解位置）
     */
    @ControllerEndpoint(exceptionMessage = "新增班主任失败", operation = "新增班主任")
    @ApiOperation(value = "新增班主任", notes = "传入班主任基本信息（姓名、性别必传）")
    @RequiresPermissions({"classAdvisor:add"})
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated ClassAdvisorVO classAdvisorVO) {
        ClassAdvisor classAdvisor = classAdvisorService.add(classAdvisorVO);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("id", classAdvisor.getId());
        resultMap.put("name", classAdvisor.getName());
        return ResponseBean.success(resultMap);
    }

    /**
     * 删除班主任（逻辑不变）
     */
    @ControllerEndpoint(exceptionMessage = "删除班主任失败", operation = "删除班主任")
    @ApiOperation(value = "删除班主任", notes = "根据ID删除指定班主任")
    @RequiresPermissions({"classAdvisor:delete"})
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) {
        classAdvisorService.delete(id);
        return ResponseBean.success();
    }

    /**
     * 班主任详情（确保返回的VO包含id，解决前端undefined问题）
     */
    @ApiOperation(value = "班主任详情", notes = "根据ID获取班主任信息，用于编辑页回显（含id）")
    @RequiresPermissions({"classAdvisor:edit"})
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id) {
        // 关键：确保ClassAdvisorVO中包含id字段，前端才能获取到editForm.id
        ClassAdvisorVO classAdvisorVO = classAdvisorService.edit(id);
        return ResponseBean.success(classAdvisorVO);
    }

    /**
     * 更新班主任信息（逻辑不变，依赖edit接口返回的id）
     */
    @ControllerEndpoint(exceptionMessage = "更新班主任失败", operation = "更新班主任")
    @ApiOperation(value = "更新班主任", notes = "根据ID更新班主任信息（姓名、性别必传）")
    @RequiresPermissions({"classAdvisor:update"})
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,
                               @RequestBody @Validated ClassAdvisorVO classAdvisorVO) {
        classAdvisorService.update(id, classAdvisorVO);
        return ResponseBean.success();
    }

    /**
     * 导出班主任Excel（优化为导出分页+条件筛选后的数据，可选）
     */
    @ApiOperation(value = "导出班主任Excel", notes = "导出当前筛选条件下的班主任信息到Excel表格")
    @RequiresPermissions({"classAdvisor:export"})
    @PostMapping("/exportExcel")
    @ControllerEndpoint(exceptionMessage = "导出班主任Excel失败", operation = "导出班主任Excel")
    // 支持导出筛选后的数据（前端传递查询条件）
    public void exportExcel(HttpServletResponse response, ClassAdvisorVO classAdvisorVO) {
        List<ClassAdvisor> list = classAdvisorService.findAllByCondition(classAdvisorVO); // 新增按条件查询所有数据的方法
        ExcelKit.$Export(ClassAdvisor.class, response).downXlsx(list, false);
    }

}